package com.zhny.aiot.xmn.water.monitor.plugin;

import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.model.IRequestMessage;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;
import com.zhny.aiot.xmn.water.monitor.plugin.model.WaterMonitorStoreEntry;

public class WaterMonitorDevice extends ModbusNumDevice<WaterMonitorStoreEntry> {
    public WaterMonitorDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super(engine, factory, centerFacilities, entity);
    }

    @Override
    public WaterMonitorStoreEntry decode(ModemResponseMessage modemResponseMessage) {
        if(!checkError (modemResponseMessage))
            logger.error ("get byte size check error !");
        logger.info ("start xmn water monitor decode !");
        WaterMonitorStoreEntry entry = new WaterMonitorStoreEntry ();
        float temperature = modemResponseMessage.getData ().readFloat ();
        float dissolvedOxygen = modemResponseMessage.getData ().readFloat ();
        float do_h = modemResponseMessage.getData ().readFloat ();
        float turbidity = modemResponseMessage.getData ().readFloat ();
        float conductivity = modemResponseMessage.getData ().readFloat ();
        float ylz = modemResponseMessage.getData ().readFloat ();
        float bga = modemResponseMessage.getData ().readFloat ();
        float cdom = modemResponseMessage.getData ().readFloat ();
        float oiw = modemResponseMessage.getData ().readFloat ();
        float cod = modemResponseMessage.getData ().readFloat ();
        float ph_mV = modemResponseMessage.getData ().readFloat ();
        float orp_mV = modemResponseMessage.getData ().readFloat ();
        float nh4_mV = modemResponseMessage.getData ().readFloat ();
        float ph = modemResponseMessage.getData ().readFloat ();
        entry.setTemperature (temperature);
        entry.setDissolvedOxygen (dissolvedOxygen);
        entry.setTurbidity (turbidity);
        entry.setConductivity (conductivity);
        entry.setPh (ph);
        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData ().readableBytes () == 56;
    }

    @Override
    protected IRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory ().getRequestMessage (getConfig ());
    }

    @Override
    protected void disposable() {

    }
}
